package edu.metrostate.capstonediner;

import android.opengl.GLSurfaceView;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.AssetManager;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.media.AudioManager;
import android.graphics.Point;

@SuppressLint("NewApi")
public class GameActivity extends Activity implements Controller {
	// Stores the view that allows us to draw via OpenGL ES
	private GLSurfaceView glView;
	// Stores the GameRenderer
	private GameRenderer renderer;
	private Game game;
	private GameInput input;
	private GameAudio audio;
	private static String user;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Hides the title bar
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// Hide all screen decorations (e.g. status bar) while this window is displayed
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		
		// Creates the view
        glView = new GLSurfaceView(this);
        setContentView(glView);
        
        // Creates the GameRenderer
        renderer = new GameRenderer(this);
        glView.setRenderer(renderer);
        
        //Creates the input object and binds to the view
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR2) {
            Point size = new Point();
        	getWindowManager().getDefaultDisplay().getSize(size);
            input = new GameInput(glView, size.x, size.y);
        }
        else{
        	Display d = getWindowManager().getDefaultDisplay();
            input = new GameInput(glView, d.getWidth(), d.getHeight());
        }
        glView.setOnTouchListener(input);
        
        //Creates audio object
        int soundSet;
        if(PreferenceManager.getDefaultSharedPreferences(this).getBoolean("altsound", false)) soundSet = Constants.ALT_SOUNDSET;
        else soundSet = Constants.DEFAULT_SOUNDSET;
        audio = new GameAudio(this, PreferenceManager.getDefaultSharedPreferences(this).getBoolean("mute", false), soundSet);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        
        // Creates and starts the Game
        game = new Game(Constants.ENDLESS_CALORIE, this, renderer, input, audio);
        game.start();
        //GameTest game = new GameTest(this);
        //game.doLoop();
	}
	
	public void onStart() {
		super.onStart();
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		// Resumes rendering
		glView.onResume();
		//resumes game
		game.unPause();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		
		// Pauses rendering
		glView.onPause();
		//Pauses game
		game.pause();
	}
	
	public void onBackPressed() {
		//do nothing; otherwise this hulking piece of shit game I've written will hang up
	}
	
	@Override
	public GameRenderer getRenderer() {
		return renderer;
	}
	
	@Override
	public GLSurfaceView getView() {
		return glView;
	}
	
	@Override
	public AssetManager getAssetManager() {
		return getAssets();
	}

	public static void setUser(String user) {
		GameActivity.user = user;
	}
	
	public static String getUser() {
		return user;
	}
}
